package com.kjlink.internal.workreport.project.service;

import cn.xuqiudong.common.base.service.BaseService;
import cn.xuqiudong.common.base.vo.Select2VO;
import com.kjlink.internal.workreport.base.enums.ProjectType;
import com.kjlink.internal.workreport.project.mapper.ProjectMapper;
import com.kjlink.internal.workreport.project.model.Project;
import com.kjlink.internal.workreport.project.vo.ProjectUser;
import com.kjlink.internal.workreport.report.service.WorkReportService;
import com.kjlink.internal.workreport.user.model.User;
import com.kjlink.internal.workreport.user.service.UserService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能: :项目 Service
 *
 * @author Vic.xu
 * @since 2024-04-29 13:39
 */
@Service
public class ProjectService extends BaseService<ProjectMapper, Project> {

    @Resource
    private UserService userService;

    @Resource
    private WorkReportService workReportService;

    @Override
    protected boolean hasAttachment() {
        return false;
    }

    /**
     * 公司下的项目 列表
     *
     * @param companyId companyId
     * @return list of project
     */
    public List<Project> optionalProject(Integer companyId) {
        Project lookup = new Project();
        lookup.setDuration(companyId);
        return this.list(lookup);
    }

    /**
     * 查询项目以及人员
     *
     * @param id 项目id
     */
    public ProjectUser projectUser(Integer id) {
        Project project = findById(id);
        if (project == null) {
            return null;
        }
        List<User> users = userService.findUsersByProjectId(id);
        return new ProjectUser(project, users);
    }

    /**
     * 新增项目人员
     *
     * @param projectId projectId
     * @param userIds   userIds
     */
    public int addProjectUser(Integer projectId, Integer[] userIds) {
        return mapper.addProjectUser(projectId, userIds);
    }

    public int deleteProjectUser(Integer userId, Integer projectId) {
        return mapper.deleteProjectUser(userId, projectId);
    }

    /**
     * 当前用户的可选项目
     * Select2VO  中的pid为jira号前缀
     *
     * @param userId 用户id
     * @return list of Project
     */
    public List<Select2VO> mineProject(Integer userId) {
        List<Select2VO> list = mapper.mineProject(userId);
        //把项目类型转换成中文
        list.forEach(select2VO -> {
            Object additional = select2VO.getAdditional();
            if (!ObjectUtils.isEmpty(additional)) {
                ProjectType byName = ProjectType.getByName(additional.toString());
                if (byName != null) {
                    select2VO.setAdditional(byName.getText());
                }
            }
        });
        return list;
    }

    /**
     * 当前用户填报使用项目
     * Select2VO  中的pid为jira号前缀
     *
     * @param userId 用户id
     * @return list of Project
     */
    public List<Select2VO> mineUsableProject(Integer userId) {
        return mapper.mineUsableProject(userId);
    }

    /**
     * 研发经理的项目
     */
    public List<Select2VO> devManagerProject(Integer userId) {
        return mapper.devManagerProject(userId);
    }


    /**
     * 统计更新项目的总工时
     *
     * @param projectId id of project
     * @return total time
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public int updateTotalTime(Integer projectId) {
        if (projectId == null) {
            return -1;
        }
        Project project = findById(projectId);
        if (project == null) {
            return -1;
        }
        Integer times = workReportService.totalTime(projectId);
        Project temp = new Project();
        temp.setId(projectId);
        temp.setUsedTime(times);
        this.update(temp);
        return times == null ? 0 : times;
    }


    public List<Project> noneClosedProjects() {
        return mapper.noneClosedProjects();
    }

}
